﻿using System.Collections.Generic;
using System.IO;
using PresentationNamespace;

namespace PresentationTests
{
    public class FakeLogger: ILogger
    {
        private List<string> log = new List<string>();
        private int loggingType = 0;

        public FakeLogger(int logType = 0)
        {
            loggingType = logType;
        }
        
        private void ToHtmlFormat(ref List<string> messages)
        {
            for (int i = 0; i < log.Count; i++)
            {
                messages[i] = string.Format("<tr><td>{0}</td></tr>", messages[i]);
            }
            messages.Insert(0, 
                "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/></head><body><table>");
            messages.Add("</table></body></html>");
        }
        
        public List<string> GetLogMessages()
        {
            var messages = new List<string>(log);

            if (loggingType == 1)
            {
                ToHtmlFormat(ref messages);
            }
            
            return messages;


        }

        public void AddMessage(string message)
        {
            log.Add(message);
        }

        public void WriteMessages(string fileName)
        {
            var logFile = new StreamWriter(fileName);
            var messages = new List<string>(log);

            if (loggingType == 1)
            {
                ToHtmlFormat(ref messages);
            }

            foreach (var message in messages)
            {
                logFile.WriteLine(message);
            }
            logFile.Close();
        }

        public List<string> GetLoggerHistory()
        {
            return log;
        }

        public void ClearLog()
        {
            log.Clear();
        }

    }
}
